//
//  AppDelegate.swift
//  GoAbroad
//
//  Created by Pink Snow on 2025/5/7.
//

import UIKit
import Clerk
import GoogleSignIn
import StripeCore
import IQKeyboardManagerSwift
import AuthenticationServices
import StoreKit

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    let clerk = Clerk.shared
    
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        clerk.configure(publishableKey: "")
        Task{
            try? await clerk.load()
        }
        IQKeyboardManager.shared.enableAutoToolbar = true
        IQKeyboardManager.shared.isEnabled = true
        IQKeyboardManager.shared.resignOnTouchOutside = true
//        SKPaymentQueue.default().add(IAPManagerOne.shared)
//        IAPManager.shared.observeTransactionUpdates()
//        let receip = IAPManager.shared.fetchReceiptBase64()
        
//                    NetworkManager.shared.requestAuthObject(.applePayVerify(receipt: receip!), type: PKTokenModal.self, using: NetworkManager.shared.authprovider).subscribe { modal in
//                        print(modal)
//
//
//                    }.disposed(by: rx.disposeBag)
        
//        printG(receip)
       ///apple登录 快速检查是否为老用户
        let appleIDProvider = ASAuthorizationAppleIDProvider()
        let userID = TokenManager.shared.appleUserIdentifier ?? ""

        appleIDProvider.getCredentialState(forUserID: userID) { state, error in
            switch state {
            case .authorized:
                print("该用户仍然授权")
            case .revoked:
                print("该用户的授权被撤销")
            case .notFound:
                print("找不到该用户")
            default:
                break
            }
        }
        
        
        
        
        GIDSignIn.sharedInstance.restorePreviousSignIn{user,error in
            if error != nil || user == nil {
                ///signed out
                printG(error)
            }else{
                ///signed in
                printG("sign in")
                
            }
        }
        
        return true
    }

    // MARK: UISceneSession Lifecycle

    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }

    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        var handled : Bool
        handled = GIDSignIn.sharedInstance.handle(url)
        if handled{
            return true
        }
        let stripeHandled = StripeAPI.handleURLCallback(with: url)
        if stripeHandled {
            return true
        }
        
        
        return false
    }
    

}

